package com.mvd.app.ac2021.activitys;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.mvd.app.R;
import com.mvd.app.activity.BaseActivity;
import com.mvd.app.activity.DaiJianCarsActivity;
import com.mvd.app.application.BaseApplication;
import com.mvd.app.commons.CommonUtils;
import com.mvd.app.commons.PDALogUtils;
import com.mvd.app.widget.MySignView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class SignActivity extends BaseActivity {

    MySignView mSignView;
    Bitmap myBitmap;
    String savePath;
    String signPath;
    public SignActivity instances = null;

    @BindView(R.id.mySignView)
    MySignView mySignView;
    @BindView(R.id.fab_clear)
    FloatingActionButton fabClear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign);
        ButterKnife.bind(this);
        String logStrf = "\n" + "onCreate-----签名-----SignActivity---" + "\n";
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStrf.getBytes());
        setBackBtn();
        setTopTitle("签名");
        setRightTxt("提交", v -> {
            saveBitmap();
            if (signPath != null){
                Log.e("tag","存在签名------signPath======"+signPath);
                Intent finishIntent = new Intent(this, DaiJianCarsActivity.class);
                finishIntent.putExtra("image", signPath);
                setResult(888,finishIntent);
//                updateSign();
            }else {
                Toast.makeText(this, "没有签名", Toast.LENGTH_SHORT).show();
                finish();
            }
        });


    }


    private void updateSign() {
        OkGo.<String>post("url").tag(this)
                .params("sign",new File(signPath))
                .isMultipart(true)
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        CommonUtils.showLoadingDialog(instances, "加载中...");
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body().trim();
//                        UtilsLog.e("上传签名照片-result==" + result);
//                        UtilsLog.e("上传签名照片-result=22=" + result.substring(1, result.length() -
//                                1));
//                        String logStr = "\n" + "上传签名照片----请求成功" + "\n" +
//                                "result::" + response.body() + "\n" +
//                                "URL::" + SharedPreferencesUtils.getIP(instances) + ITEM_START;  //接口要改
//                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
//                        CommonUtils.hideLoadingDialog(instances);
//                        String str = result.substring(1, result.length() - 1);
//                        Toast.makeText(instances, "上传签名照片成功", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
//                        String logStr2 = "\n" + "上传签名照片--onError" + "\n" +
//                                "result::" + response.body();
//                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr2.getBytes());
//                        UtilsLog.e("上传签名照片-onError==" + response.body());
//                        CommonUtils.hideLoadingDialog(instances);
                    }
                });


    }


    @OnClick({R.id.fab_clear, R.id.title_btn_left})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.fab_clear:
                mSignView.clear();
                break;
            case R.id.title_btn_left:
                mSignView.clear();
                break;
        }
    }

    @OnClick(R.id.title_btn_left)
    public void onViewClicked() {
    }

    private void saveBitmap() {

        FileOutputStream fileOutputStream = null;
        //因为xml用的是背景，所以这里也是获得背景
        myBitmap = mSignView.getBitmap();
        Log.e("tag","myBitmap========="+myBitmap);
        if (myBitmap != null) {
            try {
//            创建文件，要保存png，这里后缀就是png，要保存jpg，后缀就用jpg
//                File file = new File(Environment.getExternalStorageDirectory(), "/QuanPingQianMing/" +
//                        "Sign.png");
                File file = new File(Environment.getExternalStorageDirectory(), "/PDASign/" +
                        System.currentTimeMillis()+".png");
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                //文件输出流
                fileOutputStream = new FileOutputStream(file);
                //压缩图片，如果要保存png，就用Bitmap.CompressFormat.PNG，要保存jpg就用Bitmap.CompressFormat.JPEG,质量是100%，表示不压缩
                myBitmap.compress(Bitmap.CompressFormat.PNG, 90, fileOutputStream);
                //写入，这里会卡顿，因为图片较大
                fileOutputStream.flush();
                //记得要关闭写入流
                fileOutputStream.close();
                //成功的提示，写入成功后，请在对应目录中找保存的图片
                savePath = file.getAbsolutePath();
                Log.e("tag", "签名保存成功-----目录=======" + savePath);
//                ZhiChaoLogUtils.createLogFile(1,"手写签名保存成功".getBytes());
//            Uri imageUri = Uri.fromFile(file);
                BitmapFactory.decodeFile(savePath);//获取图片
                signPath = savePath;
            } catch (IOException e) {
                e.printStackTrace();
                //失败的提示
                Log.e("tag", e.getMessage());
            }
        }
    }
}
